﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Tools
{
    [JsonObject(MemberSerialization.OptOut)]
    public class FileList
    {
        public FileList()
        {
            List = new List<CImgList>();
            ExamList = new List<ExamResult>();
            Changed = false;
        }
        public string ToString(string type)
        {
            if (type == "")
                return string.Join("\r\n",
                    List.Select(r => string.Join("\r\n", r.ImgList)))
                    + "\r\n" +
                       string.Join("\r\n",
                    ExamList.Select(r => r.ToString()));
            if (type == "json")
            {
                return Tools.JsonFormatTool.ConvertJsonString(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            }

            return "FileList";
        }
        public static FileList JsonToFileList(string json)
        {
            try
            {
                FileList FL = Newtonsoft.Json.JsonConvert.DeserializeObject<FileList>(json);
                FL.InitDeserialize();
                return FL;
            }
            catch (Exception e)
            {
                throw e;
            }
            return null;
        }
        private void InitDeserialize()
        {
            foreach (CImgList i in List)
                i.InitDeserialize();
            foreach (ExamResult i in ExamList)
                i.InitDeserialize();
        }
        public List<CImgList> List { get; set; }
        public List<ExamResult> ExamList { get; set; }
        [JsonIgnore]
        public bool Changed { get; set; }
    }
    [JsonObject(MemberSerialization.OptOut)]
    public class CImgList
    {
        public CImgList()
        {
            ImgList = new List<string>();
        }
        public string Path { get; set; }
        public List<string> ImgList { get; set; }
        public void InitDeserialize()
        {
            if (Path != "" && Path.Contains("\\"))
                _Name = Path.Substring(Path.LastIndexOf("\\"));
            if (_Name.Contains("."))
                _Name = _Name.Substring(0, _Name.LastIndexOf("."));
            if (ImgList == null)
                ImgList = new List<string>();
        }
        public override string ToString()
        {
            return _Name;
        }
        [JsonIgnore]
        private string _Name;
    }

    [JsonObject(MemberSerialization.OptOut)]
    public class ExamResult
    {
        public ExamResult()
        {
        }
        public ExamResult(string s)
        {
            this.Path = s;
            InitDeserialize();
        }
        public void InitDeserialize()
        {
            if (Path != "" && Path.Contains("\\"))
                _Name = Path.Substring(Path.LastIndexOf("\\"));
            if (_Name.Contains("."))
                _Name = _Name.Substring(0, _Name.LastIndexOf("."));
            ImgList = new List<string>();
        }
        public override string ToString()
        {
            return PathName;
        }
        public string Path { get; set; }
        [JsonIgnore]
        public List<string> ImgList { get; set; }
        [JsonIgnore]
        public string PathName { get { return _Name; } }
        [JsonIgnore]
        private string _Name;
        //[JsonIgnore]
        //private string _Path;

    }
}
